var queryClassTermReportUrl = basePath + "/sys/lateCoach/json/pageList";
var updateStudentTermReport = basePath + "/sys/lateCoach/json/updatePlan";

var dataGrid;
$(function() {
	loadData();
	$('#files').filebox({
        buttonText: '选择文件',
        buttonAlign: 'left'
    })
});

function loadData() {
	dataGrid = $('#studentDg').datagrid({
		url : queryClassTermReportUrl,
		toolbar: '#tb',
		fit : true,
		fitColumns : true,
		border : true,
		pagination : true,
		idField : 'id',
		pageSize : config.pageSize,
		autoRowHeight : 140,
		pageList : config.pageList,
		nowrap : true,
		striped : true,
		singleSelect: true,
		rownumbers : true,
		queryParams: {
			
		},
		columns : [ [  {
			field : 'id',
			checkbox : false,
			hidden: true
		}, {
			field : 'week',
			title : '星期',
			width: 25,
			formatter : function(value,row,index){
				return row.weekText;
			},
			editor:{
				type:'combobox',
				options: {  
                    data: [{"value": "1", "text": "星期一"},{"value": "2", "text": "星期二"},{"value": "3", "text": "星期三"},{"value": "4", "text": "星期四"},{"value": "5", "text": "星期五"},{"value": "6", "text": "星期六"},{"value": "7", "text": "星期日"}],  
                    valueField: "value",  
                    textField: "text",  
                    editable: false,  
                    panelHeight:70,  
                    required: true  
                }  
			}
		}, {
			field : 'classId',
			title : '班级',
			width: 25,
			editor: {type:'textbox'}
		}, {
			field : 'coachType',
			title : '晚修',
			width: 25,
			formatter : function(value,row,index){
				return row.coachTypeText;
			},
			editor:{
				type:'combobox',
				options: {  
                    data: [{"value": "1", "text": "晚修一"},{"value": "2", "text": "晚修二"}],  
                    valueField: "value",  
                    textField: "text",  
                    editable: false,  
                    panelHeight:70,  
                    required: true  
                }  
			}
		}, {
			field : 'teacherName',
			title : '教师姓名',
			width: 25,
			editor: {type:'textbox'}
		}
		] ],
		onAfterEdit : function(rowIndex, rowData, changes){
			if(rowData.id){
				changes.id = rowData.id;
				addmodify(changes)
			}
		}
		
	});
}

function reload() {
	dataGrid.datagrid("clearSelections");
	dataGrid.datagrid('load');
}

function searchForm(formId) {
	dataGrid.datagrid('load', $.serializeObject($('#' + formId)));
}
function resetForm(formId) {
	$("#" + formId)[0].reset();
}

var uploadBox;
function upload() {
	uploadBox = $('#uploadBox').dialog({
		title : '上传晚修辅导教师名单',
		modal : true,
		width : 400,
		top : 200,
		draggable : true,
		resizable : true,
		buttons : '#btns',
		onClose : function() {
			$('#files').filebox('setValue','');
		}
	}).show();
}

function uploadCommit(){
	var fileObj = document.getElementById('filebox_file_id_2').files[0];
	if(!fileObj) {
		$.messager.alert('提醒','请选择文件');
		return;
	}
	$.messager.progress({
		title : '提示',
		msg : '上传中，请稍候...'
	});
	var xhr = new XMLHttpRequest();
	xhr.open('POST', basePath + "/sys/file/lateCoachPlan/import");
	xhr.onload = function () {
       if (xhr.status === 200) {
    	    $.messager.progress('close');
	       	var resp = JSON.parse(xhr.response);
	       	if(resp.code == '000') {
	       		$.messager.alert("成功","上传成功！","info");
	       		loadData();
	       		$('#files').filebox('setValue','');
	       		uploadBox.dialog('close');
	       	}else {
	       		$.messager.alert('错误提醒', resp.msg);
	       	}
       }
   };
   xhr.onerror = function () {
	   $.messager.progress('close');
	   $.messager.alert('错误', "上传异常");
   };
   var fd = new FormData();
   fd.append('files', fileObj);
   // 触发上传
	setTimeout(function() {
		xhr.send(fd);
	}, 1000)
}

var cleanFormPanel=function(formId){
	$('#' + formId)[0].reset();
}

function save(id) {
	if(dgEditIndex[id] != null){
		$('#' + id).datagrid('endEdit', dgEditIndex[id]);
		dgEdit[id] = null;
	}
}
function addmodify(data) {
	$.ajax({
	   type: "POST",
	   url: updateStudentTermReport,
	   data: data,
	   success: function(res){
		   if (res.code == '000'){
			   reload();
			   $.messager.alert('提示', '提交成功', 'info');
		   }else{
			   $.messager.alert('提示', '提交失败', 'error');
			   errorEdit("studentDg");
		   }
		},
		error: function() {
			alert("提交异常");
			errorEdit("studentDg");
		}
	});
}

function exportToExcel(searchForm,exportUrl) {
	$("#" + searchForm).attr('action',exportUrl);
	$("#" + searchForm).submit();  
}